import { IPSApplication, IPSAppView, IPSControl, IPSDEForm, IPSDEGrid, IPSDEToolbar, IPSModelObject, IPSSystem } from '../../exports';

/**
 * 在系统查找应用
 *
 * @author chitanda
 * @date 2022-04-20 18:04:01
 * @export
 * @param {IPSSystem} sys
 * @param {string} id
 * @return {*}  {(IPSApplication | undefined)}
 */
export function getApp(sys: IPSSystem, id: string): IPSApplication | undefined {
  return sys.getAllPSApps()?.find(item => item.id === id);
}

/**
 * 在应用查找视图
 *
 * @author chitanda
 * @date 2022-04-20 18:04:56
 * @export
 * @param {IPSApplication} app
 * @param {string} id
 * @return {*}  {(IPSAppView | undefined)}
 */
export function getView(app: IPSApplication, id: string): IPSAppView | undefined {
  return app.getAllPSAppViews()?.find(item => item.id === id);
}

/**
 * 在视图中根据名称查找部件
 *
 * @author chitanda
 * @date 2022-04-20 18:04:35
 * @export
 * @param {IPSAppView} view
 * @param {string} name
 * @return {*}  {(IPSControl | undefined)}
 */
export function getCtrl(view: IPSAppView, name: string): IPSControl | undefined {
  return view.getPSControls()?.find(item => item.name === name);
}

/**
 * 获取视图中工具栏部件
 *
 * @author chitanda
 * @date 2022-04-20 18:04:25
 * @export
 * @param {IPSAppView} view
 * @return {*}  {IPSDEToolbar}
 */
export function getToolbar(view: IPSAppView): IPSDEToolbar {
  return getCtrl(view, 'toolbar') as IPSDEToolbar;
}

/**
 * 获取视图中表格部件
 *
 * @author chitanda
 * @date 2022-04-20 18:04:42
 * @export
 * @param {IPSAppView} view
 * @return {*}  {IPSDEGrid}
 */
export function getGrid(view: IPSAppView): IPSDEGrid {
  return getCtrl(view, 'grid') as IPSDEGrid;
}

/**
 * 获取视图中表单部件
 *
 * @author chitanda
 * @date 2022-04-20 18:04:42
 * @export
 * @param {IPSAppView} view
 * @return {*}  {IPSDEForm}
 */
export function getForm(view: IPSAppView): IPSDEForm {
  return getCtrl(view, 'grid') as IPSDEForm;
}

/**
 * 从模型组组中查找指定对象
 *
 * @author chitanda
 * @date 2022-04-21 19:04:36
 * @export
 * @param {IPSModelObject[]} models
 * @param {string} id
 * @return {*}  {(IPSModelObject | undefined)}
 */
export function findModel(models: IPSModelObject[], id: string): IPSModelObject | undefined {
  return models.find(item => item.id === id);
}
